package com.chenyun.rpc.dubbo.util;

import cn.hutool.extra.spring.SpringUtil;
import com.chenyun.rpc.dubbo.dto.GenericServiceDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.utils.SimpleReferenceCache;
import org.apache.dubbo.rpc.cluster.specifyaddress.Address;
import org.apache.dubbo.rpc.cluster.specifyaddress.UserSpecifiedAddressUtil;
import org.apache.dubbo.rpc.service.GenericService;

import javax.swing.*;
import java.io.Serializable;

/**
 * dubbo 工具类
 *
 * @author: Lonni
 * @date: 2024/1/23 0023 10:49
 */
public class DubboUtil implements Serializable {

    public static final DubboUtil me = new DubboUtil();

    private DubboUtil() {
    }


    /**
     * 泛化调用
     * @param dto
     * @return
     */
    public static GenericService excuteGenericService(GenericServiceDTO dto) {
        String applicationName = SpringUtil.getApplicationName();
        String address = SpringUtil.getProperty("dubbo.registry.address");

        //创建ApplicationConfig
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(applicationName);
//        //创建注册中心配置
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(address);
//        //创建服务引用配置
        applicationConfig.setRegistry(registryConfig);
        ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
        reference.setApplication(applicationConfig);
        reference.setInterface(dto.getPackageName());
        //开启泛化调用
        reference.setGeneric(true);
        if (dto.getAsync()) {
            reference.setAsync(true);
        }
        if (!StringUtils.isBlank(dto.getVersion())) {
            reference.setVersion(dto.getVersion());
        } else {
//            reference.setVersion("1.0.0");
        }
        if (!StringUtils.isBlank(dto.getGroupName())) {
            reference.setGroup(dto.getGroupName());
        }


        reference.setTimeout(30000);
        SimpleReferenceCache cache = SimpleReferenceCache.getCache();
        try {
            GenericService genericService1 = reference.get();
            //设置为动态ip
//        UserSpecifiedAddressUtil.setAddress(new Address("192.168.56.1", 20881, true));

            GenericService genericService = cache.get(reference);
            return genericService;
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage());
        }


    }

    /**
     * 直连ip调用
     *
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> T buildService(Class<T> tClass, String ip, Integer port) {
        ReferenceConfig<T> referenceConfig = new ReferenceConfig<T>();
        //可以配置 调用失败的重试次数
        referenceConfig.setRetries(2);
        referenceConfig.setInterface(tClass);

        if (!StringUtils.isEmpty(ip) && port != null) {
            //指定ip调用
            UserSpecifiedAddressUtil.setAddress(new Address(ip, port, true));
        }
        T reference = referenceConfig.get();
        return reference;
    }


}



